<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录 - 会议管理系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
    <link rel="stylesheet" href="/static/css/style.css">
</head>
<body class="bg-light">
    <div class="container">
        <div class="auth-container">
            <div class="auth-logo">
                <i class="bi bi-calendar-event me-2"></i>会议管理系统
            </div>
            
            <div class="alert alert-danger mb-4" id="login-error" style="display: none;"></div>
            
            <div class="card border-0 shadow-sm">
                <div class="card-body p-4">
                    <h4 class="card-title text-center mb-4">用户登录</h4>
                    
                    <form id="loginForm" onsubmit="handleLogin(event)">
                        <div class="mb-3">
                            <label for="username" class="form-label">用户名</label>
                            <div class="input-group">
                                <span class="input-group-text"><i class="bi bi-person"></i></span>
                                <input type="text" class="form-control" id="username" name="username" required>
                            </div>
                        </div>
                        
                        <div class="mb-4">
                            <label for="password" class="form-label">密码</label>
                            <div class="input-group">
                                <span class="input-group-text"><i class="bi bi-lock"></i></span>
                                <input type="password" class="form-control" id="password" name="password" required>
                            </div>
                        </div>
                        
                        <div class="d-grid">
                            <button type="submit" class="btn btn-primary py-2">登录</button>
                        </div>
                    </form>
                </div>
            </div>
            
            <div class="text-center mt-4">
                <p>还没有账号？ <a href="/register" class="text-decoration-none">立即注册</a></p>
                <p class="mt-3">
                    <a href="/" class="text-decoration-none text-muted">
                        <i class="bi bi-arrow-left me-1"></i>返回首页
                    </a>
                </p>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <script src="/static/js/app.js"></script>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // 检查认证状态
            const token = localStorage.getItem('token');
            if (!token) {
                window.location.href = '/login';
                return;
            }
            
            // 设置axios默认headers
            axios.defaults.headers.common['Authorization'] = 'Bearer ' + token;
            
            // 加载数据
            getUpcomingMeetings();
            getRoomStatus();
            getStatistics();
        });

        function handleLogin(event) {
            event.preventDefault();
            
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const errorMsg = document.getElementById('login-error');
            
            if (!username || !password) {
                if (errorMsg) errorMsg.textContent = '请输入用户名和密码';
                return;
            }
            
            // 准备表单数据
            const formData = new FormData();
            formData.append('username', username);
            formData.append('password', password);
            
            // 发送登录请求
            axios.post('/token', formData, {
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                }
            })
            .then(response => {
                // 保存token并设置axios默认头
                localStorage.setItem('token', response.data.access_token);
                axios.defaults.headers.common['Authorization'] = 'Bearer ' + response.data.access_token;
                
                // 确保token设置完成后再跳转
                setTimeout(() => {
                    window.location.href = '/dashboard';
                }, 100);
            })
            .catch(error => {
                let message = '登录失败，请检查用户名和密码';
                if (error.response && error.response.data && error.response.data.detail) {
                    message = error.response.data.detail;
                }
                if (errorMsg) errorMsg.textContent = message;
            });
        }
    </script>
</body>
</html> 